package com.huifer.wgx.service.impl;

import com.huifer.wgx.model.VerifyEntity;
import com.huifer.wgx.repository.VerifyRepository;
import com.huifer.wgx.service.VerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

/**
 * <p>Title : VerifyServiceImpl </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-18
 */
@Service
public class VerifyServiceImpl implements VerifyService {

    @Autowired
    private VerifyRepository verifyRepository;
    /**
     * 保存校验规则
     *
     * @param verifyName 规则名称
     * @param verifyType 规则适用于
     * @return 校验实体 {@link VerifyEntity}
     */
    @Override
    public VerifyEntity saveVerify(String verifyName, String verifyType) {
        try {
        VerifyEntity verifyEntity = new VerifyEntity();
        verifyEntity.setVerifyName(verifyName);
        verifyEntity.setVerifyType(verifyType);

            VerifyEntity save = verifyRepository.save(verifyEntity);
            return save;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询所有校验规则
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验规则列表 {@link VerifyEntity}
     */
    @Override
    public Page<VerifyEntity> findAll(Integer pageNum, Integer pageSize) {
        Sort sort = new Sort(Direction.DESC, "verifyId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        Page<VerifyEntity> all = verifyRepository.findAll(pageable);
        return all;
    }
}
